//本题由于数量级比较小, 所以使用karaskal算法
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

class UnionFindSet {
    constructor(n) {
        this.count = n
        this.fa = new Array(n).fill(null).map((value, index) => index)
        this.rank = new Array(n).fill(0)
    }

    find(x) {
        if (this.fa[x] !== x) {
            this.fa[x] = this.find(this.fa[x])
        }
        return this.fa[x]
    }

    union(x, y) {
        let rootX = this.find(x)
        let rootY = this.find(y)

        if (rootX !== rootY) {
            //if (this.rank[rootX] > this.rank[rootY]) {
            //    this.fa[rootY] = rootX
            //} else if (this.rank[rootX] < this.rank[rootY]) {
            //    this.fa[rootX] = rootY
            //} else {
            //    this.fa[rootY] = rootX
            //    this.rank[rootX]++
            //}
            this.fa[rootY] = rootX
            this.count--
            return true
        }
        return false

    }

}


let cityNum, n, mustNum;
let initArr = [];
let mustArr = [];
rl.on("line", (line) => {
    if (cityNum === undefined) {
        [cityNum, n, mustNum] = line.trim().split(" ").map(Number)
    } else if(initArr.length < n) {
        let temp = line.trim().split(" ").map(Number)
        // 城市编号排序
        if(temp[0] > temp[1]) {
            let num = temp[0]
            temp[0] = temp[1]
            temp[1] = num
        }
        initArr.push(temp)
        if(initArr.length === n && mustNum === 0) {
            console.log(getResult(cityNum, n, mustNum, initArr, mustArr))
            // clear
            cityNum = n = mustNum = undefined
            initArr.length = 0
        }
    } else {
        let temp = line.trim().split(" ").map(Number)
        // 城市编号排序
        if(temp[0] > temp[1]) {
            let num = temp[0]
            temp[0] = temp[1]
            temp[1] = num
        }
        mustArr.push(temp)
        if (mustArr.length === mustNum) {
            console.log(getResult(cityNum, n, mustNum, initArr, mustArr))
            // clear
            cityNum = n = mustNum = undefined
            initArr.length = 0
        }
    }
})

/**
 *
 * @param cityNum {number}
 * @param n {number}
 * @param mustNum {number}
 * @param initArr {[[a, b, c]]}
 * @param mustArr {[[a, b]]}
 */
function getResult(cityNum, n, mustNum, initArr, mustArr) {
    let ans = 0
    let union = new UnionFindSet(n)
    if (mustNum > 0) {
        for (const [a, b] of mustArr) {
            for (let i in initArr) {
                const [A, B, C] = initArr[i]
                if (a === A && b === B) {
                    ans += C
                    union.union(a - 1, b - 1)
                    initArr.splice(i, 1)
                    break
                }
            }
        }
    }

    if (union.count === 1) return ans;

    // initArr 按成本排序
    initArr.sort((a, b) => a[2] - b[2])
    for (const [a, b, c] of initArr) {
        if (union.union(a - 1, b - 1)) {
            ans += c
            if (union.count === 1) break
        }
    }
    if (union.count > 1) return -1;
    return ans
}
